package com.shj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shj.mapper.SysMemberDao;
import com.shj.model.PageRequest;
import com.shj.model.PageResult;
import com.shj.model.SysMember;
import com.shj.model.SysUser;
import com.shj.util.MyIdUtil;
import com.shj.util.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysMember)表服务实现类
 *
 * @author makejava
 * @since 2020-03-12 12:42:45
 */
@Service("sysMemberService")
public class SysMemberService {
    @Resource
    private SysMemberDao sysMemberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public SysMember queryById(Integer id) {
        return this.sysMemberDao.queryById(id);
    }

    /**
     * @Description 分页查询数据
     * @author songhuajian
     * @date 2020/3/19 15:00
     */
    public PageResult findPage(PageRequest pageRequest){
        return PageUtil.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    /**
     * @Description 调用分页插件完成分页
     * @author songhuajian
     * @date 2020/3/19 14:38
     */
    public PageInfo<SysMember> getPageInfo(PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);

        List<SysMember> list = sysMemberDao.queryAll();
        return new PageInfo<SysMember>(list);
    }

    /**
     * 新增数据
     *
     * @param sysMember 实例对象
     * @return 实例对象
     */
    public SysMember insert(SysMember sysMember) {
        String memberId = MyIdUtil.getForTimeId();
        sysMember.setMemberId(memberId);
        sysMember.setMemType(0);
        this.sysMemberDao.insert(sysMember);
        return sysMember;
    }

    /**
     * 修改数据
     *
     * @param sysMember 实例对象
     * @return 实例对象
     */
    public SysMember update(SysMember sysMember) {
        this.sysMemberDao.update(sysMember);
        return this.queryById(sysMember.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id) {
        return this.sysMemberDao.deleteById(id) > 0;
    }
}